package com.numberone.system.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.numberone.system.domain.Coupon;
import com.numberone.system.mapper.CouponMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.numberone.system.mapper.CouponUserMapper;
import com.numberone.system.domain.CouponUser;
import com.numberone.system.service.ICouponUserService;
import com.numberone.common.support.Convert;

/**
 * 优惠券用户领取 服务层实现
 * 
 * @author numberone
 * @date 2020-10-09
 */
@Service
public class CouponUserServiceImpl implements ICouponUserService 
{
	@Autowired
	private CouponUserMapper couponUserMapper;
	@Autowired
	private CouponMapper couponMapper;

	/**
     * 查询优惠券用户领取信息
     * 
     * @param id 优惠券用户领取ID
     * @return 优惠券用户领取信息
     */
    @Override
	public CouponUser selectCouponUserById(Long id)
	{
	    return couponUserMapper.selectCouponUserById(id);
	}
	
	/**
     * 查询优惠券用户领取列表
     * 
     * @param couponUser 优惠券用户领取信息
     * @return 优惠券用户领取集合
     */
	@Override
	public List<CouponUser> selectCouponUserList(CouponUser couponUser)
	{
	    return couponUserMapper.selectCouponUserList(couponUser);
	}
	
    /**
     * 新增优惠券用户领取
     * 
     * @param couponUser 优惠券用户领取信息
     * @return 结果
     */
	@Override
	public int insertCouponUser(CouponUser couponUser)
	{
		couponUser.setStartTime(new Date());

		Coupon coupon = couponMapper.selectCouponById(couponUser.getCoupinId());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(couponUser.getStartTime());
		calendar.add(Calendar.DATE,coupon.getDays());

		couponUser.setEndTime(calendar.getTime());
		couponUser.setStatus(0);
		int i = couponUserMapper.insertCouponUser(couponUser);
		if (i > 0)
		{
			coupon.setTotal(coupon.getTotal() - 1);
			return couponMapper.updateCoupon(coupon);
		}
		return 0;
	}
	
	/**
     * 修改优惠券用户领取
     * 
     * @param couponUser 优惠券用户领取信息
     * @return 结果
     */
	@Override
	public int updateCouponUser(CouponUser couponUser)
	{
	    return couponUserMapper.updateCouponUser(couponUser);
	}

	/**
     * 删除优惠券用户领取对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteCouponUserByIds(String ids)
	{
		return couponUserMapper.deleteCouponUserByIds(Convert.toStrArray(ids));
	}
	
}
